package com.deng.springboot.demo.web.advice;

import com.deng.springboot.demo.web.model.vo.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 邓力宾
 * @date 2020/3/119:34
 * @Description: controller 全局异常捕获
 */
@ControllerAdvice(basePackages = {"com.deng.springboot.demo.web.controller"}) // 用于Controller 的aop通知
public class ControllerExceptionAdvice {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 异常捕获
     * @param e
     * @return
     */
    @ResponseBody // 直接返回json
    @org.springframework.web.bind.annotation.ExceptionHandler({Exception.class})
    public ResponseResult<?> handleException(Exception e ){
        logger.error( "Controller异常=>:",e);
        return  new ResponseResult<>(500, "内部错误", null);
    }
}
